package net
{
	import com.netease.protobuf.WritingBuffer;
	
	import flash.sampler.NewObjectSample;
	import flash.utils.ByteArray;
	
	import org.serialization.bson.BSON;
	
	import ua.kiev.gcore.logger.Logger;
	import ua.kiev.gcore.net.connections.Command;
	import ua.kiev.gcore.net.connections.Connection;
	import ua.kiev.gcore.net.parser.ICommandParser;
	import ua.kiev.gogames.gofishing.model.dictionaries.CommandDictionary;
	import ua.kiev.gogames.gofishing.model.net.protocol.dataCommandType.ProtobufCommandTypePackage;
	import ua.kiev.gogames.gofishing.model.net.protocol.dataPackage.ProtobufDataTypePackage;
	
	public class BSONParser implements ICommandParser
	{
		public function BSONParser()
		{
			
		}
		
		public function parse(command:*):void
		{
			var buffer:WritingBuffer = new WritingBuffer();
			buffer.writeBytes(command as ByteArray);
			buffer.position = 0;
			
			var dataPackage:ProtobufDataTypePackage = new ProtobufDataTypePackage(null, 0);
			dataPackage.readFromSlice(buffer,0);
			
			var commandPackage:ProtobufCommandTypePackage = new ProtobufCommandTypePackage(null, 0);
			commandPackage.readFromSlice(dataPackage.body, 0);
			
			var data:Object;
			try{
				data = BSON.decode(commandPackage.body);
			}catch(decodeError:Error){
				Logger.ERROR("BSONParser: " + decodeError.message);
			}
			if(data != null){
				Logger.Trace("Data parse secssesful");
				
				switch(commandPackage.type)
				{
					case CommandDictionary.Join:
					{	
						Logger.Trace("[Server] Join");
						Logger.DEBUG_OBJ(data);						
						break;
					}
					default:
					{
						Logger.Trace("[Server] ???");
						Logger.DEBUG_OBJ(data);	
						break;
					}
				}
			}
			Logger.DEBUG("parse complet");
		}
	}
}